package com.jacky.components.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;

import java.net.URL;
import java.util.ResourceBundle;

public class SpiralController implements Initializable {

    @FXML
    private AnchorPane anchorPane;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        for (double i = 0; i < 100; i = i + 0.1) {
            Line line = new Line(getSpiralXValue(i), getSpiralYValue(i),
                    getSpiralXValue(i + 0.1), getSpiralYValue(i + 0.1));

            line.setStyle("-fx-stroke: blue;");
            anchorPane.getChildren().add(line);
        }
//        this.spiral(100D,100D,50D,100,50);
    }

    public double getSpiralXValue(double x) {
        return 2.5 * x * Math.cos(x) + 250;
    }

    public double getSpiralYValue(double y) {
        return 2.5 * y * Math.sin(y) + 250;
    }

    public void spiral(double x0, double y0, double maxRadius, int
            numSegments, double spinRate) {
        double radius = 0;
        double x = x0;
        double y = y0;
        double deltaR = maxRadius / numSegments;
        for (int i = 0; i < numSegments; ++i) {
            double x1 = x;
            double y1 = y;
            radius += deltaR;
            x = x0 + radius * Math.cos((2 * Math.PI * i / numSegments) * spinRate);
            y = y0 + radius * Math.sin((2 * Math.PI * i / numSegments) * spinRate);
//            Line line = new Line(x, y, x1, y1);
//            line.setStyle("-fx-stroke: red;");
//            anchorPane.getChildren().add(line);
            // 使用StdDraw包
//            StdDraw.line(x, y, x1, y1);
        }
    }
}
